package model;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.lang.UnsupportedOperationException;

/**
 * MazeIterator allows client classes to iterate through the tiles
 * stored in a maze without any knowledge of the underlying data structure.
 * Tiles are returned in a left to right, top to bottom manner, given that
 * (0,0) is in the top left of the coordinate system, and x and y
 * increase across and down.
 * 
 * @author Alexander Craig
 */
public class MazeIterator implements Iterator<Tile> {
	
	/** the reference to the array of tiles */
	private Tile[][] tileArray;
	/** the x coordinate boundary for the array */
	private int maxX;
	/** the y coordinate boundary for the array */
	private int maxY;
	/** the x coordinate next to be retrieved */
	private int xPointer;
	/** the y coordinate next to be retrieved */
	private int yPointer;
	
	/**
	 * Constructs a new MazeIterator
	 * @param maxX	the x boundary of the passed Tile array
	 * @param maxY	the y boundary of the passed Tile array
	 * @param array	the Tile array to iterate through
	 */
	public MazeIterator(int maxX, int maxY, Tile[][] array) {
		this.maxX = maxX;
		this.maxY = maxY;
		tileArray = array;
		xPointer = 0;
		yPointer = 0;
	}
	
	@Override
	/**
	 * @return true if the next value to be retrieved will be valid
	 */
	public boolean hasNext() {
		if (xPointer >= maxX || yPointer >= maxY) {
			return false;
		}
		return true;
	}

	@Override
	/**
	 * @return the next Tile in the array
	 */
	public Tile next() {
		if (!this.hasNext()) {
			throw new NoSuchElementException();
		}
		Tile returnTile = tileArray[xPointer][yPointer];
		xPointer++;
		if ((xPointer = xPointer % maxX) == 0) {
			yPointer++;
		}
		return returnTile;
	}

	@Override
	/**
	 * Not implemented.
	 */
	public void remove() {
		throw new UnsupportedOperationException();
	}

}
